package net.elswear;

import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

import net.elswear.Page;
import net.elswear.PMF;

@SuppressWarnings("serial")
public class AdminServlet extends HttpServlet {
	
	private static final Logger log = Logger.getLogger(IndexServlet.class.getName());
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
	{
		response.sendRedirect("admin.jsp");
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException 
	{
		UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
		
        String name = request.getParameter("name");
        String content = request.getParameter("content");
        String title = request.getParameter("title");
        
        Key key = KeyFactory.createKey(Page.class.getSimpleName(), name);
        
        Date date = new Date();
        Page page = new Page(name, user, content, title, date);
        page.setKey(key);
        
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(page);
        } finally {
            pm.close();
        }
        
		response.sendRedirect("admin.jsp");	
	}
}
